/*!	brief The face class is used to represent an object's face. A face is, for now, always a GL_TRIANGLES
 
 		Brief Workflow Overview : The face class take 3 vertex pointers. To create a new vector, just give a face references to previously created vertices and use the
 		static function "New". When a face is created, it will automaticcaly fill an Index Buffer wich will then be sent to the IBO Buffer in order to speed rendering
 */
 
Face	=	function (vertexA, vertexB, vertexC)
{
	/****************************************
	/*				Properties				
	/****************************************/
	
		/*! Offset of the Index in the m_Index array. This data is very important for glDrawElements, and is set during vector to ushort conversion	*/
		
		this.m_Offset; 		
		this.m_Mesh;		/*!< Mesh Linked to current Face	*/
		this.m_Vertices = new Array();         /*!< Vertices connected to the current face	*/
		
		
		this.m_Index;            /*!< Index of connected vertices. Could be used for IBO creation	*/

		this.m_Normal;           /*!< Surface's normal												*/
		
	/****************************************
	/*				Constructor				
	/****************************************/
	
		this.m_Vertices.push(vertexA);
		this.m_Vertices.push(vertexB);
		this.m_Vertices.push(vertexC);
		
		this.ComputeNormal();
		console.log(this.m_Normal);
		
}
/*!	brief Compute the normal of the current face. As a face is always a triangle, it only took 2 points and a cross product to determine it	*/
Face.prototype.ComputeNormal	=	function()
{
	vectorAB 	= 	this.m_Vertices[1].m_Attributes.m_Attributes[0].Minus(this.m_Vertices[0].m_Attributes.m_Attributes[0]);
	vectorAC	= 	this.m_Vertices[2].m_Attributes.m_Attributes[0].Minus(this.m_Vertices[0].m_Attributes.m_Attributes[0]);
	
	this.m_Normal = Vec4.Cross(vectorAB, vectorAC);
	this.m_Normal= this.m_Normal.Normalize();
}

Face.prototype.ReturnTransformedPosition	=	function(matrix)
{
	array = new Array();
	array.push(matrix.MultVec4( this.m_Vertices[0].m_Attributes.m_Position));
	array.push(matrix.MultVec4( this.m_Vertices[1].m_Attributes.m_Position));
	array.push(matrix.MultVec4( this.m_Vertices[2].m_Attributes.m_Position));
	return array;
}

Face.Faces = new Array();      /*!< Store all application's faces			*/

/*!	brief Create a new face, storing references to it's connected vertices,
   and add the newly created face in top of static vector Faces                */
Face.New = function(vertexA, vertexB, vertexC)
{
	Face.Faces.push( new Face(vertexA, vertexB, vertexC));
	return Face.Faces[Face.Faces.length-1];
}

/*! brief The array method will tranform the Faces vector so it can be passed to the IBO
   To do so, we inspect all connected faces, get back theirs index, and put it inside an array */
Face.Array = function ()
{
	Face.index = new Array();
	
	var f=0;
	var g=0;
			
	for(var i=0;i< Face.Faces.length; i++)
	{
		g=f;
		for(var j=0; j<Face.Faces[i].m_Vertices.length; j++)
		{
			f++;
			Face.Faces[i].m_Offset=g;

			Face.index.push(Face.Faces[i].m_Vertices[j].m_Index);
		}

	}
	return Face.index;
}





